package com.roger.myHome.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roger.myHome.domain.Ticket;
import com.roger.myHome.domain.TicketLog;
import com.roger.myHome.mapper.TicketLogMapper;
import com.roger.myHome.mapper.TicketMapper;
import com.roger.myHome.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.RedissonMultiLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private RedissonClient redissonClient;

    @Resource
    private TicketMapper ticketMapper;

    @Resource
    private TicketLogMapper ticketLogMapper;

    private static final String LOCK_NAME = "anyRWLock";

    //static int TICK_NUM = 50;




    @Override
    public void doRedisLock () {
        String[] lockKey = {"lock1", "lock2", "lock3"};

        RLock [] rLocks = new RLock[lockKey.length];
        for(int i = 0,length = lockKey.length; i < length ;i ++){
            RLock lock = redissonClient.getLock(lockKey[i]);
            rLocks[i] = lock;
        }
        RedissonMultiLock multiLock = new RedissonMultiLock(rLocks);
        multiLock.lock(10, TimeUnit.SECONDS);
        try {
            String thread = Thread.currentThread().getName();
            //
            QueryWrapper<Ticket> wrapper = new QueryWrapper<>();
            wrapper.eq("id", 0);
            Ticket ticket = ticketMapper.selectOne(wrapper);
            //
            if(ticket.getNums() == 0){
                System.err.println(thread + "===========ticket is out");
                return;
            }

            //
            String content = (new Date()) + thread + "抢到票，还剩" + ticket.getNums() + "张";
            log.info("{}", content);
            TicketLog ticketLog = new TicketLog();
            ticketLog.setLeftTicket(ticket.getNums());
            ticketLog.setName(thread);
            ticketLogMapper.insert(ticketLog);
            //这个是购票代码
            TimeUnit.SECONDS.sleep(5);
            Integer left = ticket.getNums() - 1;
            Ticket updateObj = new Ticket();
            updateObj.setNums(left);
            updateObj.setId(0);
            ticketMapper.updateById(updateObj);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            multiLock.unlock();
        }

    }

    @Override
    public void doInsert () {
        try {

            String content = "ddd";
            TicketLog ticketLog = new TicketLog();
            ticketLog.setLeftTicket(5);
            ticketLog.setName("ttt");
            ticketLogMapper.insert(ticketLog);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static boolean method1() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Method 1: " + i);
            try {
                Thread.sleep(1000); // 模拟耗时操作
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public static boolean method2() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Method 2: " + i);
            try {
                Thread.sleep(1000); // 模拟耗时操作
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static void main2(String[] args) throws Exception{
        long begin = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(2);

// 提交任务1
        Future<Boolean> future1 = executorService.submit(() -> method1());
        // 提交任务2
        Future<Boolean> future2 = executorService.submit(() -> method2());

        // 获取任务1的结果
        Boolean result1 = future1.get();
        // 获取任务2的结果
        Boolean result2 = future2.get();

        // 输出结果
        System.out.println("Method 1 result: " + result1);
        System.out.println("Method 2 result: " + result2);

        long end = System.currentTimeMillis();
        System.out.println("导入耗时：" + (end - begin) / 1000 + "s");
    }

    public static void main(String[] args) {
        List<Integer> receiver = new ArrayList<>();
        receiver.add(1);
        receiver.add(2);

        List<Integer> request = new ArrayList<>();
        request.add(1);
        request.add(3);

        receiver.addAll(request);
        System.out.println(receiver);

        List<Integer> uniqueList = receiver.stream()
                .distinct()
                .collect(Collectors.toList());

        System.out.println(uniqueList);

    }
}
